#include <iostream>
#include <winsock2.h>
#include<ws2tcpip.h>
#include <string>
#include "data.h"
#include "cmd.h"
#include "frame.h"

#define MAX_FRAME_LEN 512
WSADATA wsaData;
SOCKET sockStream = INVALID_SOCKET;
struct addrinfo *result = NULL,hints;


#define DEFAULT_PORT "9051" //服务器端口
int main(int argc, char** argv) {
	//1.初始化

	int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		std::cout << "failed" << iResult << std::endl;
		return 1;
	}
	//初始化结束
	ZeroMemory(&hints,sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;
	//从本机中获取ip地址等信息为了socket使用
	iResult = getaddrinfo(NULL,DEFAULT_PORT,&hints,&result);
	sockStream = WSASocket(result->ai_family,result->ai_socktype,result->ai_protocol,NULL,0,WSA_FLAG_OVERLAPPED);
	
	
	//绑定套接字
	iResult = bind(sockStream,result->ai_addr,(int)result->ai_addrlen);
	if(iResult == SOCKET_ERROR){
		std::cout<<"bind failed with error:"<<WSAGetLastError()<<std::endl;
		freeaddrinfo(result);
		closesocket(sockStream);
		WSACleanup();
		return 1;
	}
	//4.在套接字上监听客户端
	if(listen(sockStream,SOMAXCONN) == SOCKET_ERROR){
		std::cout<<"Listen failed with error:"<<WSAGetLastError()<<std::endl;
		closesocket(sockStream);
		WSACleanup();
		return 1;
	}
	SOCKET ClientSocket; //临时套接字对象，用来接受来自客户端的连接
	// 这里没有考虑多线程并发接受客户端 只接受一个
	ClientSocket = accept(sockStream,NULL,NULL);
	if(ClientSocket == INVALID_SOCKET){
		std::cout<<"accept failed:"<<WSAGetLastError()<<std::endl;
		closesocket(sockStream);
		WSACleanup();
		return 1;
	}
	
	char recvbuf[MAX_FRAME_LEN];
	int iSendResult;
	int recvbuflen = MAX_FRAME_LEN;
	
	do{
		iResult = recv(ClientSocket,recvbuf,recvbuflen,0);
		if(iResult > 0){
//			std::cout<<"Bytes received("<<iResult<<")"<<recvbuf<<std::endl;
			//接收并判断发送
			ext_game_robot_status_t *recvdata;
			recvdata = (ext_game_robot_status_t *)(recvbuf+sizeof(framehdr)+2);
//			char *robotname = new char(20);
			std::string robotname,robotlevel,robothp;
			if(recvdata->robot_id == 1){
				robotname = "英雄机器人";
			}
			if(recvdata->robot_level==1){
				robotlevel = "等级1";
			}
			if(recvdata->remain_HP==0){
				robothp="机器人血量为0";
			}
			std::cout<<robotname<<" "<<robotlevel<<" "<<robothp<<std::endl;
			
			//返回发送
			uint16_t cmd_id = EXT_GAME_RESULT;
			int datasize = 0;
			switch (cmd_id) {
			case EXT_GAME_STATUS:
				datasize = 11;
				break;
			case EXT_GAME_RESULT:
				datasize = 1;
				break;
			}
			
			char frame_data[MAX_FRAME_LEN];
			memset(frame_data,0,MAX_FRAME_LEN);
			framehdr* frame_header;
			ext_game_result_t *data;
			//填充头部
			frame_header = (framehdr*)frame_data;
			frame_header->SOF =0xA5;
			frame_header->daga_length = datasize;
			frame_header->seq = 1;
			//填充cmd命令
			*(frame_data+sizeof(framehdr)) = cmd_id;
			//填充数据段
			data = (ext_game_result_t*)(frame_data+sizeof(framehdr)+sizeof(cmd_id));
			data->winner = 2;
			//发送数据
			iSendResult = send(ClientSocket,frame_data,7+datasize,0);
			if(iSendResult == SOCKET_ERROR){
				std::cout<<"send failed:"<<WSAGetLastError()<<std::endl;
				closesocket(ClientSocket);
				WSACleanup();
				return 1;
			}
			std::cout<<"比赛结果发送给客户端"<<frame_data<<std::endl;
			
		}else if(iResult==0){
			std::cout<<"Connection closing...."<<std::endl;
		}else{
			std::cout<<"recv failed:"<<WSAGetLastError()<<std::endl;
			closesocket(ClientSocket);
			WSACleanup();
			return 1;
		}
		
	}while(iResult >0);
	
	// 7.断开连接 start
	iResult = shutdown(ClientSocket, SD_SEND);
	if (iResult == SOCKET_ERROR) {
		std::cout<<"shutdown failed:"<<WSAGetLastError()<<std::endl;
		closesocket(ClientSocket);
		WSACleanup();
		return 1;
	}
	
	
	
	
	
	
	return 0;
}
